package com.simtop.controller;

import com.github.pagehelper.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.UUID;


public class TestLogger {

    public  static Logger logger= LoggerFactory.getLogger(TestLogger.class);

    public static void main(String[] args) {
        for (int i = 0; i <4000; i++) {
            StringBuilder sb = new StringBuilder();
            String loginName = UUID.randomUUID().toString().substring(0,12);
            int experimentPreview = new Random().nextInt(250) + 250;
            int experimentSafe = new Random().nextInt(250) + 250;
            int experimentOrganization = new Random().nextInt(500) + 500;
            int equipmentDismantLing = new Random().nextInt(500) + 500;
            int principle = new Random().nextInt(500) + 500;
            int design = new Random().nextInt(2000)+500;
            int sampleTesting = new Random().nextInt(1000)+1000;
            int processOptimization = new Random().nextInt(500)+500;
            int totalScore = experimentPreview+experimentSafe+experimentOrganization+equipmentDismantLing
                    +principle+design+sampleTesting+processOptimization;

            Date date=randomDate("2021-04-13","2021-07-30");
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String format = sdf.format(date);
            logger.warn("loginName:{},experimentPreview:{},experimentSafe:{}," +
                    "experimentOrganization:{},equipmentDismantLing:{},principle:{},design:{},sampleTesting:{},processOptimization:{},totalScore:{}" +
                    "date:{}",loginName, String.format("%.2f",experimentPreview*0.01),String.format("%.2f",experimentSafe*0.01),String.format("%.2f",experimentOrganization*0.01),
                    String.format("%.2f",equipmentDismantLing*0.01),String.format("%.2f",principle*0.01),String.format("%.2f",design*0.01),
                    String.format("%.2f",sampleTesting*0.01),String.format("%.2f",processOptimization*0.01),String.format("%.2f",totalScore*0.01),format);
            /*if(i%20==0){
                logger.error("[Druid-ConnectionPool-Create-259587740] ERROR com.alibaba.druid.pool.DruidDataSource - create connection SQLException, url: jdbc:mysql://localhost:3306/rotor_spinning?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&useSSL=false, errorCode 1045, state 28000\n" +
                        "java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)\n" +
                        "\tat com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)\n" +
                        "\tat com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)\n" +
                        "\tat com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:833)\n" +
                        "\tat com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:453)\n" +
                        "\tat com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:246)\n" +
                        "\tat com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:198)\n" +
                        "\tat com.alibaba.druid.pool.DruidAbstractDataSource.createPhysicalConnection(DruidAbstractDataSource.java:1570)\n" +
                        "\tat com.alibaba.druid.pool.DruidAbstractDataSource.createPhysicalConnection(DruidAbstractDataSource.java:1636)\n" +
                        "\tat com.alibaba.druid.pool.DruidDataSource$CreateConnectionThread.run(DruidDataSource.java:2505)");
            }
            if(i%30==0){
                logger.error("10:02:06.622 [http-nio-8877-exec-4] ERROR o.a.c.c.C.[.[.[.[dispatcherServlet] - [log,175] - Servlet.service() for servlet [dispatcherServlet] in context with path [/car] threw exception [Request processing failed; nested exception is org.json.JSONException: JSONObject[\"question_nums\"] not found.] with root cause\n" +
                        "org.json.JSONException: JSONObject[\"question_nums\"] not found.\n" +
                        "\tat org.json.JSONObject.get(JSONObject.java:471)\n" +
                        "\tat org.json.JSONObject.getInt(JSONObject.java:598)\n" +
                        "\tat com.simtop.car.controller.UserController.submitGrades(UserController.java:142)\n" +
                        "\tat sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\n" +
                        "\tat sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)\n" +
                        "\tat sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)\n" +
                        "\tat java.lang.reflect.Method.invoke(Method.java:498)\n" +
                        "\tat org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190)\n" +
                        "\tat org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)\n" +
                        "\tat org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105)\n" +
                        "\tat org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:879)\n" +
                        "\tat org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:793)\n" +
                        "\tat org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)\n" +
                        "\tat org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040)\n" +
                        "\tat org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)\n" +
                        "\tat org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)\n" +
                        "\tat org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909)\n" +
                        "\tat javax.servlet.http.HttpServlet.service(HttpServlet.java:660)\n" +
                        "\tat org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)\n" +
                        "\tat javax.servlet.http.HttpServlet.service(HttpServlet.java:741)\n" +
                        "\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)\n" +
                        "\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\n" +
                        "\tat org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)\n" +
                        "\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\n" +
                        "\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\n" +
                        "\tat org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)\n" +
                        "\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)\n" +
                        "\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\n" +
                        "\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\n" +
                        "\tat org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)\n" +
                        "\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)\n" +
                        "\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\n" +
                        "\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\n" +
                        "\tat org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)\n" +
                        "\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)\n" +
                        "\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\n" +
                        "\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\n" +
                        "\tat org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)\n" +
                        "\tat org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)\n" +
                        "\tat org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)\n" +
                        "\tat org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)\n" +
                        "\tat org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)\n" +
                        "\tat org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)\n" +
                        "\tat org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)\n" +
                        "\tat org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:373)\n" +
                        "\tat org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)\n" +
                        "\tat org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)\n" +
                        "\tat org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1590)\n" +
                        "\tat org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)\n" +
                        "\tat java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)\n" +
                        "\tat java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)\n" +
                        "\tat org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)\n" +
                        "\tat java.lang.Thread.run(Thread.java:748)");
            }*/
        }
    }


    /**
     * 获取随机时间
     * @param beginDate
     * @param endDate
     * @return
     */
    private static Date randomDate(String beginDate,String endDate){
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Date start = format.parse(beginDate);
            Date end = format.parse(endDate);

            if(start.getTime() >= end.getTime()){
                return null;
            }

            long date = random(start.getTime(),end.getTime());

            return new Date(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private static long random(long begin,long end){
        long rtn = begin + (long)(Math.random() * (end - begin));
        if(rtn == begin || rtn == end){
            return random(begin,end);
        }
        return rtn;
    }

}
